<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class member extends CI_Controller {

	function __construct(){
            parent::__construct();
            $this->load->library(array('session','form_validation'));
            $this->load->helper(array('form', 'url'));
            $this->member_id = $this->session->userdata('member_id');
            $this->member_nama = $this->session->userdata('member_nama');
            $this->load->model('peneliti_model');
            $this->load->model('fakultas_model');
            $this->load->model('jurusan_model');
            $this->load->model('skim_model');
            $this->load->model('program_model');
            $this->load->model('penelitian_model');
        }
	
	public function index()
	{
                if($this->member_id)
                {
                    redirect("member/detail");
//                    $data["aa"]="";
//                    $this->load->view('template/header');
//                    $this->load->view('member/member');
//                    $this->load->view('member/sidebar', $data);
//                    $this->load->view('template/footer');
                }else{
                    redirect("member/login");
                }

	}
        
        public function reg_penelitian()
	{
            if($this->member_id)
            {
                $skim_id = $this->input->post('skim_id');
                
                $this->form_validation->set_message('required', "Anda wajib memilih %s sebelum melanjutkan pendaftaran");
		$this->form_validation->set_rules('skim_id','SKIM','trim|required');
                
		if ($this->form_validation->run() == FALSE)
		{
                    $petunjuk = "Pilih Program, lalu pilih SKIM sebelum mendaftarkan data penelitian lebih lanjut";
                    $data["data_petunjuk"] = $petunjuk;
                    $data["data_peneliti"] = $this->peneliti_model->get_peneliti($this->member_id);
                    $data["data_skim"] = $this->skim_model->get_skim();
                    $data["data_program"] = $this->program_model->get_program();
                    $data["data_tahun"] = $this->program_model->get_list_tahun();
                    $this->load->view('template/header'); 
                    $this->load->view('penelitian/register_step1',$data);
                    $this->load->view('member/sidebar', $data);
                    $this->load->view('template/footer');
                }
                else
                {
                    redirect("member/reg_penelitian2/$skim_id");
                }
            }else{
                    redirect("member/login");
                }

	}
        
        public function reg_penelitian2()
	{
            if($this->member_id)
            {
                $skim = $this->uri->segment(3);
                $judul = $this->input->post('judul');
                $abstraksi = $this->input->post('abstr');
               
                
                $biaya1 = $this->input->post('biaya1');
                $biaya2 = $this->input->post('biaya2');
                $biaya3 = $this->input->post('biaya3');
                $luaran = $this->input->post('luaran');
                
                $this->form_validation->set_message('required', "%s harus diisi");
		$this->form_validation->set_rules('judul','Judul Penelitian','trim|required');
                $this->form_validation->set_rules('abstr','Abstraksi Penelitian','trim|required');
                $this->form_validation->set_rules('biaya1','Biaya Penelitian tahun 1','trim|required');
                $this->form_validation->set_rules('luaran','Luaran Penelitian','trim|required');
                $petunjuk = "Isi data penelitian dengan benar lalu klik tombol daftar";
                $data["data_petunjuk"] = $petunjuk;
                $data["data_peneliti"] = $this->peneliti_model->get_peneliti($this->member_id);
                $data["data_skim"] = $this->skim_model->get_skim_from_id($skim);
                
                $data["data_peneliti_all"] = $this->peneliti_model->get_peneliti_all();
                $data["list_jurusan"] = $this->jurusan_model->get_jurusan_fakultas();
                $data_skim = $this->skim_model->get_skim_from_id($skim)->result();
                $data["data_program"] = $this->program_model->get_prog_from_id($data_skim[0]->PROGRAM_ID);
                $max_anggota = $data_skim[0]->SKIM_MAX_ANGGOTA;
                
                
                
		if ($this->form_validation->run() == FALSE)
		{
                    
                    
                    $this->load->view('template/header'); 
                    $this->load->view('penelitian/register_step2',$data);                    
                    $this->load->view('member/sidebar', $data);
                    $this->load->view('template/footer');
                }
                else
                {
                    //insert judul, abstrak, biaya, luaran
                    $this->penelitian_model->insert_penelitian($skim,$this->member_id, $judul, $abstraksi,$biaya1,$biaya2,$biaya3,$luaran);
                    $penelitian_id_baru = $this->db->insert_id();
                    $this->penelitian_model->insert_anggota($penelitian_id_baru,$this->member_id,1);
                    
                    for($i=1; $i<$max_anggota;$i++)
                    {
                        if($this->input->post('id_peneliti'.$i)){
                            //insert anggota
                            $this->penelitian_model->insert_anggota($penelitian_id_baru,$this->input->post('id_peneliti'.$i),2);
                            
                        }
                    }
                    redirect("member/reg_penelitian3/".$penelitian_id_baru);
                }
            }
	}
        
        public function reg_penelitian3()
	{
            $penelitian = $this->uri->segment(3);
            if($this->member_id && $this->peneliti_model->cek_penelitian_milik($penelitian,$this->member_id))
            {
                $data["penelitian_id"] = $penelitian;
                $petunjuk = "Pastikan file yang anda upload berformat pdf";
                $data["data_petunjuk"] = $petunjuk;
                $this->load->view('template/header'); 
                $this->load->view('penelitian/register_step3',$data);                    
                $this->load->view('member/sidebar', $data);
                $this->load->view('template/footer');
            }
            else{
                redirect("member");
            }
        }
        
        public function upload_proposal(){
                    $penelitian_id = $this->input->post('penelitian_id');
                    $this->load->library('upload');  
                    $config['upload_path'] = './static/docs/doc_penelitian'; 
                    $config['allowed_types'] = 'pdf';  
                    $config['max_size']  = '0';
                    $config['file_name'] = "proposal".$penelitian_id;
                    $config['overwrite']  = TRUE;
                    $this->upload->initialize($config);
                    $upload = $this->upload->do_upload();

                    if($upload)
                    {
                        $data = $this->upload->data();
                        $path = $data['file_name'];
//                        $this->skim_model->insert_panduan_path($last_id,$path);
                    }
                    else {
                        echo 'gagal';
//                        redirect('member/reg_penelitian3/'.$penelitian_id);
                    }
        }


        public function login(){
            if(!$this->member_id)
            {
                $email = $this->input->post('email');
                $pass = $this->input->post('pass');
                
                $this->form_validation->set_message('required', "%s harus diisi");
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('pass', 'Password', 'trim|required');

		if ($this->form_validation->run() == FALSE)
		{
                    $data["ket"] = ""; 
                    $this->load->view('template/header'); 
                    $this->load->view('member/login',$data);
                    $this->load->view('template/footer');
		}
		else
		{
                    $data_member = $this->peneliti_model->cek_login($email, $pass);
                    if($data_member){
                        $this->session->set_userdata('member_id',$data_member->PENELITI_ID);
                        $this->session->set_userdata('member_nama',$data_member->PENELITI_NAMA);
                        $data["ket"] = "sukses login $data_member->PENELITI_NAMA";
                        redirect('member');
                    }
                    else {
                        $data["ket"] = "username password tidak valid";
                        $this->load->view('template/header', $data);
                        $this->load->view('member/login', $data);
                        $this->load->view('template/footer', $data);
                    }
                }
            }
            else{
                redirect("member");
            }
        }
        
        public function register(){
            $nama = $this->input->post('nama');
            $pass = $this->input->post('pass');
            $gelar_depan = $this->input->post('gelar_depan');
            $gelar_belakang = $this->input->post('gelar_belakang');
            $jk = $this->input->post('jk');
            $tgl = $this->input->post('tgl');
            $email = $this->input->post('email');
            $nohp = $this->input->post('nohp');
            $alamat = $this->input->post('alamat');
            $nidn = $this->input->post('nidn');
            $nip = $this->input->post('nip');
            $jurusan = $this->input->post('jurusan');
            $jabatan = $this->input->post('jabatan');
            $golongan = $this->input->post('golongan');
            
            $data['submit_data']=array(
                'nama' => $this->input->post('nama'),
                'gelar_depan' => $this->input->post('gelar_depan'),
                'gelar_belakang' => $this->input->post('gelar_belakang'),
                'jk' => $this->input->post('jk'),
                'tgl' => $this->input->post('tgl'),
                'email' => $this->input->post('email'),
                'nohp' => $this->input->post('nohp'),
                'alamat' => $this->input->post('alamat'),
                'nidn' => $this->input->post('nidn'),
                'nip' => $this->input->post('nip'),
                'jurusan' => $this->input->post('jurusan'),
                'jabatan' => $this->input->post('jabatan'),
                'golongan' => $this->input->post('golongan')
            );
            
            $this->form_validation->set_message('required', "%s wajib di isi");
            $this->form_validation->set_message('min_length', "%s harus lebih dari 6 karakter");
            $this->form_validation->set_message('max_length', "%s tidak boleh lebih dari 12 karakter");
            $this->form_validation->set_message('valid_email', "%s tidak valid");
            $this->form_validation->set_message('numeric', "%s tidak valid, isikan nomor yang benar");
            
            $this->form_validation->set_rules('nama', 'Nama', 'trim|required');
            $this->form_validation->set_rules('jk', 'Jenis Kelamin', 'required');
            $this->form_validation->set_rules('tgl', 'Tanggal Lahir', 'trim|required');
            $this->form_validation->set_rules('email', 'Email', 'trim|valid_email|required');
            $this->form_validation->set_rules('nohp', 'Nomor Hp', 'trim|required|numeric');
            $this->form_validation->set_rules('alamat', 'Alamat', 'trim|required');
            $this->form_validation->set_rules('nidn', 'NIDN', 'trim|required|numeric');
            $this->form_validation->set_rules('nip', 'NIP', 'trim|required|numeric');
            $this->form_validation->set_rules('jurusan', 'Jurusan', 'trim|required');
            $this->form_validation->set_rules('golongan', 'Golongan', 'trim|required');
            $data["list_jurusan"] = $this->jurusan_model->get_jurusan_fakultas();
            
            
            
            if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('template/header', $data);
                $this->load->view('member/register', $data);
                $this->load->view('template/footer', $data);
            }
            else
            {
                $this->peneliti_model->insert_peneliti($nama,$pass,$gelar_depan,$gelar_belakang,$email,$nohp,$alamat,$nidn,$nip,$jurusan,$jabatan,$golongan,$jk,$tgl);
                $last_id = $this->db->insert_id();
                
                $this->load->library('upload');  
                $this->load->library('image_lib'); 
                $config['upload_path'] = './static/images/member/photo'; 
                $config['allowed_types'] = '*';  
                $config['max_size']  = '0';  
                $config['max_width']  = '0';  
                $config['max_height']  = '0';  
                $config['overwrite']  = TRUE;
                $this->upload->initialize($config);
                $upload = $this->upload->do_upload();
                
                if($upload)
                {
                    $data = $this->upload->data();
                    $path = $data['file_name'];
                    $this->peneliti_model->insert_path_foto($path,$last_id);
                }
                
                $this->load->view('template/header', $data);
                $this->load->view('member/register_finished', $data);
                $this->load->view('template/footer', $data);
            }
        }
        
        public function edit(){
            
            if($this->member_id)
            {  
                $peneliti_id = $this->member_id;
                $nama = $this->input->post('nama');
                $pass = $this->input->post('pass');
                $gelar_depan = $this->input->post('gelar_depan');
                $gelar_belakang = $this->input->post('gelar_belakang');
                $jk = $this->input->post('jk');
                $tgl = $this->input->post('tgl');
                $email = $this->input->post('email');
                $nohp = $this->input->post('nohp');
                $alamat = $this->input->post('alamat');
                $nidn = $this->input->post('nidn');
                $nip = $this->input->post('nip');
                $jurusan = $this->input->post('jurusan');
                $jabatan = $this->input->post('jabatan');
                $golongan = $this->input->post('golongan');

                $data['submit_data']=array(
                    'nama' => $this->input->post('nama'),
                    'gelar_depan' => $this->input->post('gelar_depan'),
                    'gelar_belakang' => $this->input->post('gelar_belakang'),
                    'email' => $this->input->post('email'),
                    'nohp' => $this->input->post('nohp'),
                    'alamat' => $this->input->post('alamat'),
                    'nidn' => $this->input->post('nidn'),
                    'nip' => $this->input->post('nip'),
                    'jabatan' => $this->input->post('jabatan'),
                    'golongan' => $this->input->post('golongan'),
                    'jk' => $this->input->post('jk'),
                    'tgl' => $this->input->post('tgl'),
                );

                $this->form_validation->set_message('required', "%s wajib di isi");
                $this->form_validation->set_message('min_length', "%s harus lebih dari 6 karakter");
                $this->form_validation->set_message('max_length', "%s tidak boleh lebih dari 12 karakter");
                $this->form_validation->set_message('valid_email', "%s tidak valid");
                $this->form_validation->set_message('numeric', "%s tidak valid, isikan nomor yang benar");

                $this->form_validation->set_rules('nama', 'Nama', 'trim|required');
                $this->form_validation->set_rules('jk', 'Jenis Kelamin', 'required');
                $this->form_validation->set_rules('tgl', 'Tanggal Lahir', 'trim|required');
                $this->form_validation->set_rules('email', 'Email', 'trim|valid_email|required');
                $this->form_validation->set_rules('nohp', 'Nomor Hp', 'trim|required|numeric');
                $this->form_validation->set_rules('alamat', 'Alamat', 'trim|required');
                $this->form_validation->set_rules('nidn', 'NIDN', 'trim|required|numeric');
                $this->form_validation->set_rules('nip', 'NIP', 'trim|required|numeric');
                $this->form_validation->set_rules('jurusan', 'Jurusan', 'trim|required');
                $this->form_validation->set_rules('golongan', 'Golongan', 'trim|required');
                $data["list_jurusan"] = $this->jurusan_model->get_jurusan_fakultas();
                $data["data_peneliti"] = $this->peneliti_model->get_peneliti($this->member_id);

                $petunjuk = "Ini adalah halaman edit data, silahkan update data terbaru anda. <br/> klik tombol Edit untuk menyetujui edit data";
                $data["data_petunjuk"] = $petunjuk;
                if ($this->form_validation->run() == FALSE)
                {
                    $this->load->view('template/header', $data);
                    $this->load->view('member/edit', $data);
                    $this->load->view('member/sidebar', $data);
                    $this->load->view('template/footer', $data);
                }
                else
                {
                    $this->peneliti_model->update_peneliti($peneliti_id, $nama,$gelar_depan,$gelar_belakang,$email,$nohp,$alamat,$nidn,$nip,$jurusan,$jabatan,$golongan,$jk,$tgl);
                   

                    $this->load->library('upload');  
                    $this->load->library('image_lib'); 
                    $config['upload_path'] = './static/images/member/photo'; 
                    $config['allowed_types'] = '*';  
                    $config['max_size']  = '0';  
                    $config['max_width']  = '0';  
                    $config['max_height']  = '0';  
                    $config['overwrite']  = TRUE;
                    $this->upload->initialize($config);
                    $upload = $this->upload->do_upload();

                    if($upload)
                    {
                        $data = $this->upload->data();
                        $path = $data['file_name'];
                        $this->peneliti_model->insert_path_foto($path,$this->member_id);
                    }
                    redirect("member");
                }
            }
            else{
                redirect("member");
            }
        }
        
        public function detail(){
            if($this->member_id)
            {
                $petunjuk = "Selamat datang di SIM Penelitian ITS. Selamat melakukan penelitian.</br> </br> Salam LPPM ITS";
                $data["data_petunjuk"] = $petunjuk;
                $data["data_peneliti"] = $this->peneliti_model->get_peneliti($this->member_id);
                $this->load->view('template/header', $data);
                $this->load->view('member/detail', $data);
                $this->load->view('member/sidebar', $data);
                $this->load->view('template/footer', $data);
            }
            else{
                redirect("member");
            }
        }
        
        public function list_penelitianku(){
            if($this->member_id)
            {
                
                $petunjuk = "Ini adalah halaman daftar penelitian anda.";
                $data["data_petunjuk"] = $petunjuk;
                $data["penelitianku"] = $this->penelitian_model->get_penelitian_by_peneliti($this->member_id);
                $this->load->view('template/header', $data);
                $this->load->view('member/list_penelitianku', $data);
                $this->load->view('member/sidebar', $data);
                $this->load->view('template/footer', $data);
            }
            else{
                redirect("member");
            }
        }
        
        public function view_penelitianku(){
            if($this->member_id)
            {
                $penelitian_id = $this->input->post('penelitian_id');
                $petunjuk = "Ini adalah halaman penelitian anda.";
                $data["data_petunjuk"] = $petunjuk;
                $data["penelitianku"] = $this->penelitian_model->get_penelitian_by_peneliti($penelitian_id);
                $data["timpeneliti"] = $this->penelitian_model->get_timpenelitian($penelitian_id);
                $this->load->view('template/header', $data);
                $this->load->view('penelitian/view_penelitianku', $data);
                $this->load->view('member/sidebar', $data);
                $this->load->view('template/footer', $data);
            }
            else{
                redirect("member");
            }
        }
        
        public function edit_pass(){
            if($this->member_id)
            {
                
                $email = $this->input->post('email');
                $passold = $this->input->post('passold');
                $passnew = $this->input->post('passnew');
                
                $data['submit_data']=array(
                    'email' => $this->input->post('email'),
                    'passold' => $this->input->post('passold'),
                    'passnew' => $this->input->post('passnew')
                );

                $this->form_validation->set_message('required', "%s wajib di isi");
                $this->form_validation->set_message('valid_email', "%s tidak valid");
                $this->form_validation->set_message('numeric', "%s tidak valid, isikan nomor yang benar");
                $this->form_validation->set_rules('email', 'Email', 'trim|required');
                $this->form_validation->set_rules('passold', 'Password Lama', 'required');
                $this->form_validation->set_rules('passnew', 'Password Baru', 'trim|required');
                
                $petunjuk = "Halaman ini adalah halaman untuk mengganti password.</br> </br> Jangan sampai anda lupa dengan password anda";
                $data["data_petunjuk"] = $petunjuk;
                $data["data_peneliti"] = $this->peneliti_model->get_peneliti($this->member_id);
                
                if ($this->form_validation->run() == FALSE)
                {
                    $this->load->view('template/header', $data);
                    $this->load->view('member/editpass', $data);
                    $this->load->view('member/sidebar', $data);
                    $this->load->view('template/footer', $data);
                }
                else
                {
                    $this->peneliti_model->change_pwd($this->member_id, $email, $passold, $passnew);
                    $this->load->view('template/header', $data);
                    $this->load->view('member/editpass', $data);
                    $this->load->view('member/sidebar', $data);
                    $this->load->view('template/footer', $data);
                    redirect("member");
                }
                
            }
            else{
                redirect("member");
            }
        }

        public function logout(){
            if($this->member_id)
            {
                $this->session->sess_destroy();
                redirect('member');
            }
            redirect(" ");
        }
        
        
        
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */